package com.riven.bilibilidemo.fragment;

import android.content.Intent;
import android.content.res.AssetManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.activity.LiveAppIndexActivity;
import com.riven.bilibilidemo.activity.RegionTypeDetailsActivity;
import com.riven.bilibilidemo.bean.PartitionRecyclerViewAdapter;
import com.riven.bilibilidemo.bean.RegionTypesInfo;
import com.riven.bilibilidemo.utils.UIUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/*
*@date 创建时间 2017年3月10日08:00:05
*@作者：孙亚东
*@作用：分区页面
*/


public class PartitionFragment extends BaseFragment {

    private RecyclerView recyclerView;
    private List<RegionTypesInfo.DataBean> regionTypes = new ArrayList<>();
    private PartitionRecyclerViewAdapter partitionRecyclerViewAdapter;

    @Override
    public int layoutID() {
        return R.layout.fragment_partition;
    }

    @Override
    protected void initViews(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.partition_RecyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
        partitionRecyclerViewAdapter = new PartitionRecyclerViewAdapter(getActivity());
        recyclerView.setAdapter(partitionRecyclerViewAdapter);
    }

    @Override
    protected void initData() {
        Observable.just(readAssetsJson())
                .map(s -> new Gson().fromJson(s, RegionTypesInfo.class))
                .map(RegionTypesInfo::getData)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(dataBeans -> {
                    regionTypes.addAll(dataBeans);
//                    UIUtils.getToast(dataBeans.toString());
                }, throwable -> {
                });
        initListener();
    }

    private void initListener() {
        partitionRecyclerViewAdapter.setOnItemClickListener(new PartitionRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void ItemClickListener(int position) {
                switch (position){
                    case 0:
                        startActivity(new Intent(getActivity(), LiveAppIndexActivity.class));
                        break;
                    case 1:
                        RegionTypesInfo.DataBean mBangumi = regionTypes.get(1);
                        RegionTypeDetailsActivity.launch(getActivity(), mBangumi);
                        break;
                    case 2:
                        RegionTypesInfo.DataBean mAnimation = regionTypes.get(2);
                        RegionTypeDetailsActivity.launch(getActivity(), mAnimation);
                        break;
                    case 3:
                        RegionTypesInfo.DataBean mMuise = regionTypes.get(3);
                        RegionTypeDetailsActivity.launch(getActivity(), mMuise);
                        break;
                    case 4:
                        RegionTypesInfo.DataBean mDence = regionTypes.get(4);
                        RegionTypeDetailsActivity.launch(getActivity(), mDence);
                        break;
                    case 5:
                        RegionTypesInfo.DataBean mGame = regionTypes.get(5);
                        RegionTypeDetailsActivity.launch(getActivity(), mGame);
                        break;
                    case 6:
                        RegionTypesInfo.DataBean mScience = regionTypes.get(6);
                        RegionTypeDetailsActivity.launch(getActivity(), mScience);
                        break;
                    case 7:
                        RegionTypesInfo.DataBean mLife = regionTypes.get(7);
                        RegionTypeDetailsActivity.launch(getActivity(), mLife);
                        break;
                    case 8:
                        RegionTypesInfo.DataBean mKichiku = regionTypes.get(8);
                        RegionTypeDetailsActivity.launch(getActivity(), mKichiku);
                        break;
                    case 9:
                        RegionTypesInfo.DataBean mFashion = regionTypes.get(9);
                        RegionTypeDetailsActivity.launch(getActivity(), mFashion);
                        break;
                    case 10:
                        break;
                    case 11:
                        RegionTypesInfo.DataBean mRecreation = regionTypes.get(10);
                        RegionTypeDetailsActivity.launch(getActivity(), mRecreation);
                        break;
                    case 12:
                        RegionTypesInfo.DataBean mMovei = regionTypes.get(11);
                        RegionTypeDetailsActivity.launch(getActivity(), mMovei);
                        break;
                    case 13:
                        RegionTypesInfo.DataBean mTv = regionTypes.get(12);
                        RegionTypeDetailsActivity.launch(getActivity(), mTv);
                        break;
                }
            }
        });
    }

    /**
     * 读取assets下的json数据啊
     */
    private String readAssetsJson() {

        AssetManager assetManager = getActivity().getAssets();
        try {
            InputStream is = assetManager.open("region.json");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder stringBuilder = new StringBuilder();
            String str;
            while ((str = br.readLine()) != null) {
                stringBuilder.append(str);
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
